/*
* @Description:合约管理-上传
* @Author: lil
* @Date: 2021/11/24
**/
<template>
  <div>
    <!-- 表单 -->
    <el-form :model="form" :rules="rules" ref="form" label-width="118px">
      <el-form-item label="合约名称:" prop="contractName">
        <el-input v-model="form.contractName" :disabled="readOnly" placeholder="请输入合约名称"></el-input>
      </el-form-item>
      <el-form-item label="合约接口名称:" prop="contractTnterface">
        <el-input v-model="form.contractTnterface" placeholder="如：interface1,interface2"></el-input>
      </el-form-item>
      <el-form-item label="版本号:" prop="version">
        <el-input v-model="form.version" :disabled="readOnly" placeholder="请输入版本号"></el-input>
      </el-form-item>
      <el-form-item label="合约:" prop="file">
        <el-upload class="upload-demo" action="#" :before-upload="beforeUpload" accept=".go" :on-change="fileChange"
          :on-remove="fileRemove" :http-request="handleRequest" :file-list="form.fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <span>(只能上传1个格式为.go的文件！)</span>
        </el-upload>
      </el-form-item>
      <el-form-item label-width="0" class="btns">
        <el-button type="primary" v-html="operation" @click="uploadSure"></el-button>
        <el-button type="info" @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import {
    uploadCCSure,
    uploadCC
  } from "@/api/blockchain/blockchaindeploy";
  export default {
    props: ["operation", "versionCode", "chainCodeName"],
    data() {
      let checkVersion = (rule, value, callback) => {
        let reg = /^\d+\.\d+$/;
        if (value === '') {
          callback(new Error('请输入版本号！'));
        } else if (!reg.test(value)) {
          callback(new Error('请输入正确的版本号!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          //合约名称
          contractName: "",
          //版本号
          version: "",
          // 合约结构体
          contractTnterface: "",
          // 结构体键值
          // structureKeyValue: '',
          fileList: [],
          file: null,
          pdfData: {
            uploadFile: "",
          },
        },
        rules: {
          contractName: [{
            required: true,
            message: "合约名称不能为空！",
            trigger: "change",
          }, ],
          contractTnterface: [{
            required: true,
            message: "合约接口名称不能为空！",
            trigger: "change",
          }, ],
          version: [{
            validator: checkVersion,
            trigger: "change",
          }, ],
          file: [{
            required: true,
            message: "请上传合约！",
            trigger: "change",
          }, ],
        },
        readOnly: false
      };
    },
    mounted() {
      this.initContractInfo();
    },
    methods: {
      initContractInfo() {
        if (this.versionCode) {
          this.readOnly = true;
          this.form.contractName = this.chainCodeName;
          this.form.version = parseInt(this.versionCode) + 1 + '.0';
        }
      },
      handleRequest(file) {
        var formdata = new FormData();
        formdata.append("uploadFile", file.file);
        uploadCC(formdata)
          .then(function (res) {})
          .catch((err) => {
            console.log(err);
          });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        if (this.readOnly) {
          this.form.contractName = this.chainCodeName;
          this.form.version = parseInt(this.versionCode) + 1 + '.0';
        }
        this.form.fileList = [];
      },
      fileChange(uploadFile, fileList) {
        this.form.file = uploadFile;
        var testmsg = this.form.file.name.substring(this.form.file.name.lastIndexOf(".") + 1);
        const extension = testmsg === "go";
        if (fileList.length !== 0) {
          this.$refs.form.validateField("file");
          if (fileList.length > 1 && extension) {
            fileList.splice(0, 1);
          }
        }
      },
      fileRemove(uploadFile, fileList) {
        if (fileList.length === 0) {
          this.form.file = null;
        }
      },
      beforeUpload() {
        var testmsg = this.form.file.name.substring(this.form.file.name.lastIndexOf(".") + 1);
        const extension = testmsg === "go";
        if (!extension) {
          this.$message({
            message: "上传文件只能是.go格式!",
            type: "warning",
          });
        }
        return extension;
      },
      uploadSure() {
        this.form.contractTnterface = this.form.contractTnterface.replace(
          /\s*/g,
          ""
        );

        this.$refs.form.validate((valid) => {
          if (valid) {
            var _this = this;
            uploadCCSure(
                this.form.contractName,
                this.form.version,
                this.form.contractTnterface,
                this.readOnly
              )
              .then(function (res) {
                if (_this.readOnly) {
                  _this.$message.success("新合约上传成功！");
                  _this.$emit("close-dialog");
                } else {
                  _this.$message.success("文件上传成功！");
                  _this.$emit("close-uploadDialog");
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-form {
    padding: 40px 40px 10px;

    &>>>.el-form-item__label {
      &::before {
        content: "*";
        display: inline-block;
        color: #f12945;
      }
    }

    .btns {
      text-align: center;
      margin-top: 70px;

      .el-button:nth-of-type(1) {
        margin-right: 40px;
      }
    }
  }

</style>
<style lang="scss">
  .upload-demo {
    max-height: 60px;

    .el-upload-list {
      width: 50%;
    }
  }

</style>
